﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Marbles.Core.Controls
{
    [ToolboxData("<{0}:GoogleAnalytics runat=server></{0}:GoogleAnalytics>")]
    public class GoogleAnalytics : WebControl
    {

        public string AccountId { get { object obj = ViewState["AccountId"]; return (obj == null ? "" : obj.ToString()); } set { ViewState["AccountId"] = value; } }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute("type", "text/javascript");
            writer.AddAttribute("language", "javascript");
            writer.RenderBeginTag(HtmlTextWriterTag.Script);
            writer.WriteLine("var _gaq = _gaq || [];");
            writer.WriteLine("_gaq.push(['_setAccount', '" + this.AccountId + "']);");
            writer.WriteLine("_gaq.push(['_trackPageview']);");
            writer.WriteLine("(function() {");
            writer.WriteLine("var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;");
            writer.WriteLine("ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';");
            writer.WriteLine("var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);");
            writer.WriteLine("})();");
            writer.RenderEndTag();

        }


    }
}
